//
//  UITableView+CYTableView.m
//  cyjf
//
//  Created by tauCross on 2017/5/17.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "UITableView+CYTableView.h"


static char cy_tableView_auto_lock_vertical_key;



@implementation UITableView (CYTableView)

+ (void)load
{
    Method oriMethod = class_getInstanceMethod([self class], @selector(layoutSubviews));
    Method cyMethod = class_getInstanceMethod([self class], @selector(cy_layoutSubviews));
    method_exchangeImplementations(oriMethod, cyMethod);
}

- (void)setCy_autoLockVertical:(BOOL)cy_autoLockVertical
{
    objc_setAssociatedObject(self, &cy_tableView_auto_lock_vertical_key, @(cy_autoLockVertical), OBJC_ASSOCIATION_COPY);
    if(cy_autoLockVertical)
    {
        [self setNeedsLayout];
    }
    else
    {
        self.scrollEnabled = YES;
    }
}

- (BOOL)cy_autoLockVertical
{
    return [objc_getAssociatedObject(self, &cy_tableView_auto_lock_vertical_key) boolValue];
}

- (void)cy_layoutSubviews
{
    [self cy_layoutSubviews];
    if(self.cy_autoLockVertical)
    {
        @weakify(self);
        dispatch_async(dispatch_get_main_queue(), ^{
            @strongify(self);
            if(self)
            {
                if(self.contentSize.height > self.height)
                {
                    self.scrollEnabled = YES;
                }
                else
                {
                    self.scrollEnabled = NO;
                }
            }
        });
    }
    else
    {
        self.scrollEnabled = YES;
    }
}

@end
